/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.report.accounting;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import src.DBManager;
import src.domain.gl.Glbalance;
import src.domain.system.Branch;
import src.report.AbstractBaseReportBean;
import src.services.CRUEDService;
import src.view.system.CompanyBean;

/**
 *
 * @author Lalugina
 */
@ManagedBean
@SessionScoped
public class RptGlbalanceBean extends AbstractBaseReportBean{
    private final int REPORT_TRIAL          = 0;
    private final int REPORT_BALANCE_PERIOD = 1;
    private final int REPORT_BALANCE_MONTH  = 2;
    private final String COMPILE_FILE_NAME_TRIAL = "gl/trialbalance";
    private final String COMPILE_FILE_NAME_PERIOD = "gl/periodbalance";
    private final String COMPILE_FILE_NAME_MONTH = "gl/monthbalance";
    
    private List<Glbalance> Glbalances;
    private Date startDate, endDate;
    private Integer branchId, year;
    private int reportId;

    public RptGlbalanceBean() {
    }

    @Override
    protected JRDataSource getJRDataSource() {
        RptGlbalanceDS ds = new RptGlbalanceDS(Glbalances);
        return ds;
    }

    @Override
    protected String getCompileFileName() {
        String file = null;
        switch(reportId) {
            case REPORT_TRIAL: file = COMPILE_FILE_NAME_TRIAL;break;
            case REPORT_BALANCE_PERIOD: file = COMPILE_FILE_NAME_PERIOD;break;
            case REPORT_BALANCE_MONTH: file = COMPILE_FILE_NAME_MONTH;break;
        }
        return file;
    }
    
    public String execute(List<Glbalance> reports) {
	try {
            String branchName = "Semua Cabang";
            if (branchId>0) branchName = ((Branch) new CRUEDService().getById(Branch.class, branchId)).getBranchName();
            
            String period;
            if (this.reportId != REPORT_BALANCE_MONTH)
                period = src.util.Utils.formatDate(startDate).concat(" s/d ")
                    .concat(src.util.Utils.formatDate(endDate)).concat(" ").concat(branchName);
            else period = "Period ".concat(year.toString()).concat(" ").concat(branchName); 
            
            super.getReportParameters().put("p_company", new CompanyBean().loadCompanyInfo().toString());
            super.getReportParameters().put("p_period", period);
            
            DBManager db = new DBManager();
            db.getConnection();
            super.getReportParameters().put("REPORT_CONNECTION", db.conn);
            this.Glbalances = reports;
            super.prepareReportViaConn(getJRDataSource());
            db.putConnection();
	} catch (JRException | IOException e) {}
	
        return null;
    }

    public List<Glbalance> getGlbalances() {
        return Glbalances;
    }

    public void setGlbalances(List<Glbalance> Glbalances) {
        this.Glbalances = Glbalances;
    }

    public Integer getBranchId() {
        return branchId;
    }

    public void setBranchId(Integer branchId) {
        this.branchId = branchId;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    public int getReportId() {
        return reportId;
    }

    public void setReportId(int reportId) {
        this.reportId = reportId;
    }
    
}
